package ink.metoo.auto.fishpi.chatroom.command

import ink.metoo.auto.fishpi.Log
import ink.metoo.auto.fishpi.Settings
import ink.metoo.auto.fishpi.call.ChatRoomCall
import ink.metoo.auto.fishpi.chatroom.Message
import ink.metoo.auto.fishpi.chatroom.RedPacket
import org.apache.commons.lang3.math.NumberUtils

open class SendRedPacketCommand(val type: RedPacket.Type) : Command {

    override val commandText: String = "/${type.description}"

    override fun isMatch(message: Message): Boolean {
        return super.isMatch(message) && message.isMe
    }

    override fun execute(message: Message) {
        val params = message.commandParams
        val count = NumberUtils.toInt(params.getOrNull(1), type.minCount).coerceAtLeast(type.minCount)
        val money = NumberUtils.toInt(params.getOrNull(2), 32).coerceAtLeast(32)
        val result = ChatRoomCall.sendRedPacket {
            it.msg = params.getOrNull(3) ?: "${Settings.fishpiClient.username}的${type.description}"
            it.money = money
            it.count = count
            it.recivers = emptyList()
            it.type = type.value
        }
        Log.info("发送了${money}${type.description}, 发送结果: ${result.code == 0}")
    }


}